#include <brpc/server.h>
#include <butil/logging.h>
#include "../common/Etcd.hpp"
#include "main.pb.h"

using namespace example;
using namespace XKL;

class EchoServiceImpl : public example::EchoService
{
public:
    void Echo(google::protobuf::RpcController* controller, 
            const ::example::EchoRequest* request,
            EchoResponse* response,
            google::protobuf::Closure* done) override
    {
        brpc::ClosureGuard rpc_guard(done);
        std::cout << "get message: " << request->message() << std::endl;
        std::string str = request->message() + "--response";
        response->set_message(str);
    }
};

int main()
{
    initLogger("stdout", "", 1);
    // 关闭brpc的默认日志输出
    logging::LoggingSettings log_settings;
    log_settings.logging_dest = logging::LoggingDestination::LOG_TO_NONE;
    logging::InitLogging(log_settings);
    // 构造服务器对象
    brpc::Server server;
    // 向服务器对象中，新增EchoService对象
    EchoServiceImpl echo_service;
    int ret = server.AddService(&echo_service, brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE);
    if(ret == -1)
    {
        std::cout << "server add failed" << std::endl;
        return -1;
    }
    brpc::ServerOptions options;
    options.idle_timeout_sec = -1;
    ret = server.Start(7070, &options);
    if(ret == -1)
    {
        std::cout << "server start failed" << std::endl;
        return -1;
    }

    XKL::RpcRegister::ptr rclient = std::make_shared<XKL::RpcRegister>("http://127.0.0.1:2379");
    rclient->onRegister("/service/echo/instance", "127.0.0.1:7070");

    server.RunUntilAskedToQuit();
    return 0;
}